<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link type="text/css" rel="stylesheet" href="css/login.css"/>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="js/login.js"></script>
    <title>注册</title>
</head>

<body>
<div class="header">
    <div class="mainwarp">
        <div class="logo"><a href="index.jsp"><img src="images/log.png"/></a></div>
        <div class="welcome">欢迎注册</div>
        <div class="headerright">
            <a href="login.jsp">个人登录</a>
            <span>|</span>
            <a href="index.jsp">返回首页</a>
        </div>
    </div>
</div>
<div class="zhuceBox">
    <div class="zcLeft">
        <ul id="tabzc">
            <li>邮箱/手机号注册</li>
            <li></li>
            <div class="clear"></div>
        </ul>
        <div class="clear"></div>
        <div id="contentzc">
            <ul style="display:block;">
                <div class="zcpart0">
                    <div class="zcpart5">
                        <span></span>
                        <input id="PhoneOrEmail" name="PhoneOrEmail" type="text" placeholder="手机号或邮箱" value=""/>
                    </div>
                    <div id="registrationPrompt" class="zcpart6">手机号/邮箱用于登录和找回密码</div>
                    <div class="zcpart7">
                        <span></span>
                        <input id="registPassword" name="" type="text" placeholder="设置密码"/>
                    </div>
                    <div id="passwordAuthentication" class="zcpart6">请输入6-20个字符</div>
                    <div class="zcpart2_c">
                        <span id="xieyi" class="CheckBox Yes"></span>
                        <span style="float:left">我已阅读并接受<a href="user_agreement.jsp">用户协议</a></span>
                    </div>
                    <div class="clear"></div>
                    <div id="RegisterButton" class="zcpart8">
                        <a>注 册</a>
                    </div>
                    <div class="zcpart8 zcpart9">
                        <a href="resume.jsp">一分钟填写简历 快速填写 省时省力</a>
                    </div>
                </div>
            </ul>
        </div>
    </div>
    <div class="zcRight">
        <div class="topdiv">
            <span class="noPa">已有账号了？</span><br/>
            <a href="login.jsp">立即登录</a>
        </div>
    </div>
</div>
<div class="clear"></div>
</body>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">

    var regemial = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
    var regphone = new RegExp("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-9])|(18[0-9])|166|198|199|191|(147))\\d{8}$");
    var uphone = null;
    var uemail = null;
    var j = null;

    function stringValidation(phoneOrEmail, upassword) {

        formatVerification(phoneOrEmail);
        if (j == 1) {
            if (uemail != null) {
                $.ajaxSettings.async = false
                $.get("registUser.do",
                    "&uemail=" + uemail +
                    "&upassword=" + upassword, function (i) {
                        if (i != null) {
                            window.location.replace("login.jsp")
                        }
                    })
            }
            if (uphone != null) {
                $.ajaxSettings.async = false
                $.get("registUser.do",
                    "uphone=" + uphone +
                    "&upassword=" + upassword, function (i) {
                        if (i != null) {
                            window.location.replace("login.jsp")
                        }
                    })
            }

        }


    }

    function availabilityOrNot(obj) {
        if (obj != "") {
            $("#registrationPrompt").html("手机号或邮箱已被使用")
            return 0;
        } else if (obj == "") {
            $("#registrationPrompt").html("手机号或邮箱可以使用")
            return 1;
        }

    }


    function formatVerification(phoneOrEmail) {
        if (regphone.test(phoneOrEmail)) {
            uphone = phoneOrEmail
            $.get("verificationUser.do", "uphone=" + phoneOrEmail, function (obj) {
                j = availabilityOrNot(obj)
            })
        } else if (regemial.test(phoneOrEmail)) {
            uemail = phoneOrEmail
            $.get("verificationUser.do", "uemail=" + phoneOrEmail, function (obj) {
                j = availabilityOrNot(obj)

            })
        }
    }


    $(document).ready(function () {
        $("[id='RegisterButton']").click(function () {

            if ($("#xieyi").hasClass("CheckBox Yes")) {
                var phoneOrEmail = $("[id='PhoneOrEmail']").val()
                var password = $("[id='registPassword']").val()

                if (phoneOrEmail == "" || password == "" || password.length > 20 || password.length < 6) {
                    if (phoneOrEmail == "") {
                        $("#registrationPrompt").html("手机号或邮箱不能为空")
                    }
                    if (password == "") {
                        $("#passwordAuthentication").html("密码不能为空")
                    }
                    if (password.length > 20 || password < 6) {
                        $("#passwordAuthentication").html("密码长度为6-20")
                    }
                } else {
                    stringValidation(phoneOrEmail, password);
                }
            }
            else {
                alert("请勾选用户协议")
            }
        })

        $("[id='PhoneOrEmail']").blur(function (str) {
            var phoneOrEmail = $(this).val();
            formatVerification(phoneOrEmail)
        })
    })
</script>
</html>
